package com.Modular.pdf_Util.examplesPDF.AnQuanSheShiJianCha;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import com.Modular.MinioPlugin.MinIoOperations;
import com.Modular.pdf_Util.PDF_utils;
import com._Config.Model.errorLogModel;
import com.jfinal.core.Controller;
import com.jfinal.log.Log4jLog;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.*;

/**
 * 车辆调度日志登记
 */
public class AnQuanSheShiJianChaService extends Controller {
    static Log4jLog logger = Log4jLog.getLog(AnQuanSheShiJianChaService.class);

    String dir = System.getProperty("user.dir");
    String fileName = String.format("%s%s.pdf", "安全生产检查表", DateUtil.format(new Date(), "yyyyMMddHHmmss"));
    String outPath = String.format("%s%s%s", dir, "/webapp/WEB-INF/template/temp/", fileName);
    public static final AnQuanSheShiJianChaService service = new AnQuanSheShiJianChaService();

    public String buildPdf(String id) {

        try {
            String time = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
            errorLogModel model=errorLogModel.dao.findById(id);
            Map<String, String> titleMap =new HashMap<>();
            if(model!=null){
            titleMap=modelToMap(model);
            }
            List<Map<String, Object>> mapList = xiangMuToMap(model);

            String pdfName = String.format("%s.pdf", "安全生产检查表-" + time);
            //生成本地pdf 一表单+一表格
            PDF_utils.buildPdf("安全生产检查表-模板.docx", outPath, titleMap, 1, mapList);
            logger.debug("fileName:%s,output:%s", fileName, outPath);
            MinIoOperations.fileUpload("asiatrip", pdfName, outPath);
            String geturl = MinIoOperations.fileGeturlGet("asiatrip", pdfName);

            String SYS_DanWeiMingCheng=model.getStr("SYS_DanWeiMingCheng");
            String danWeiDaiMa=model.getStr("SYS_DanWeiDaiMa");
            //关连电子档案
            dangAnWenJian("安全生产检查表",pdfName,geturl,null,SYS_DanWeiMingCheng,danWeiDaiMa);
            return geturl;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public Map<String, String> modelToMap(errorLogModel model) {
        Map<String, String> data = new HashMap<>();
        data.put("QiYeMingCheng", model.getStr("QiYeMingCheng"));
        data.put("JianChaBuMen", model.getStr("JianChaBuMen"));
        data.put("ShouJianBuMen", model.getStr("ShouJianBuMen"));
        data.put("JianChaRenYuanLeiXing",model.getStr("JianChaRenYuanLeiXing")==null?"":model.getStr("JianChaRenYuanLeiXing"));
        data.put("JianChaRiQi", DateUtil.parse(model.getStr("JianChaRiQi")).toString("yyyy-MM-dd HH:mm"));
        data.put("JianChaFuZeRen", model.getStr("JianChaFuZeRen")==null?"":model.getStr("JianChaFuZeRen"));

        String time = DateFormatUtils.format(new Date(), "yyyy-MM-dd");
        data.put("Time", time);
        return data;
    }
    public List<Map<String, Object>> xiangMuToMap(errorLogModel model) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        String jianChaJson = model.getStr("JianChaNeiRong");
        if (jianChaJson != null) {
            JSONArray json = JSONUtil.parseArray(jianChaJson);
            for (int i = 0; i < json.size(); i++) {
                Map<String, Object> data = new HashMap<>();
                JSONObject m= json.getJSONObject(i);
//                String key= (String) m.keySet().toArray()[0];
                String value= m.getStr("CanShuMingCheng");
                String jieGuo= m.getStr("JieGuo");
                data.put("key",value);
                data.put("value",jieGuo==null?"":jieGuo);
                data.put("XiangMu",model.getStr("JianChaLeiXing"));
                data.put("num",i);
                mapList.add(data);
            }
        }
        return mapList;
    }

    //关连电子档案
    public void dangAnWenJian(String muLuName, String fileName, String filePath, String SYS_DanWeiDianNaoBianHao,
                              String SYS_DanWeiMingCheng, String SYS_DanWeiDaiMa) {
        //创建文件记录（电子档案）
    	errorLogModel fileModel = new errorLogModel();
        fileModel.set("Id", IdUtil.randomUUID());
        fileModel.set("ZhuId", "3ee497d5-5146-42fb-b3cd-e3d8527c53bc");//安全生产检查
        fileModel.set("WenJianZiLei",muLuName);
        fileModel.set("WenJianLeiBie","隐患排查与治理");
        fileModel.set("WenJianMingCheng", fileName);
        fileModel.set("WenJianLaiYuan", "系统生成");
        fileModel.set("WenJianWeiZhi", filePath);
        fileModel.set("SYS_DanWeiDianNaoBianHao", SYS_DanWeiDianNaoBianHao);
        fileModel.set("SYS_DanWeiMingCheng", SYS_DanWeiMingCheng);
        fileModel.set("SYS_DanWeiDaiMa", SYS_DanWeiDaiMa);
        fileModel.set("GengXinShiJian", new Date());
        fileModel.set("BianHao", 1);
        fileModel.save();
    }
}
